'use strict';
angular.module('invoice.payFKHX', [])
	.controller('payFKHXCtrl',['$rootScope', '$scope', 'BizData', 'systemService', f])
    function f($rootScope, $scope, BizData, systemService) {

		var Model = this;
		
		var common = require('common');
		
		//客户信息
		$scope.customerID = null;
		$scope.customerNo = '';

	    //获取客户信息
	    Model.customerOnBlur = function(event) {
//			console.log('event-->',event);
//			console.log('customerNo-->',$scope.customerNo);
			if($scope.customerNo==null || $scope.customerNo==''){
				return;
			};
			
			/*这两种形式是相同的效果
			var params = {
					"customerNo":$scope.customerNo,
					"async":false //异步？
			};
			*/
			
			var params = {};
			params.customerNo = $scope.customerNo;
			params.async = false;
			
			
			$('#customerNo').LoadingOverlay("show");
			
			var response = systemService.getProviderByNo(params);
			if(response!=null){
				if (response.code != 0){
	         		$scope.customerID = null;
//	         		$scope.customerNo = null;
	         		$scope.customerName = null;

//					alert("请在单据设置中进行初始化。");
					systemService.dialogMsg(response.status);
				}else{
	         		$scope.customerID = response.data.fID;
	         		$scope.customerNo = response.data.providerNo;
	         		$scope.customerName = response.data.providerName;
             		$scope.enterpriseNo = response.data.enterpriseNo;
             		$scope.enterpriseName = response.data.enterpriseName;
				}
			}
			
			$('#customerNo').LoadingOverlay("hide");

		};
		
		//打开对话框（选择供应商）
		Model.triggerCustomerNoClick = function(){
			
			var options = {};
			
			options.recordState = 'none';

			var modalInstance = systemService.openProviderNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
	             		$scope.customerID = returnData[i].fID;
	             		$scope.customerNo = returnData[i].providerNo;
	             		$scope.customerName = returnData[i].providerName;
	             		$scope.enterpriseNo = returnData[i].enterpriseNo;
	             		$scope.enterpriseName = returnData[i].enterpriseName;
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
		};
		
		
		
		Model.dataMaster1AfterRefresh = function(event) {
			var dataMaster1 = event.source;
			for(var i=0;i<dataMaster1.getCount();i++){
				var rowID = dataMaster1.getID(i);
				var pendingAmount = dataMaster1.getValue('pendingAmount',rowID);
				dataMaster1.setValue('orderAmountCleared',pendingAmount,rowID);
			}
		};
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		Model.dataMaster1ValueChanged = function(event) {
			console.log('dataMaster1ValueChanged.event-->>',event);
			if(event.column=='orderAmountCleared'){
				var dataMaster1 = event.source;
				var rowID = dataMaster1.getID(event.rowIndex);
				var orderAmountCleared = dataMaster1.getValue('orderAmountCleared',rowID);
				var pendingAmount = dataMaster1.getValue('pendingAmount',rowID);
				if(orderAmountCleared>pendingAmount){
					dataMaster1.setValue('orderAmountCleared',pendingAmount,rowID);
				}
				Model.checkbox1ValueChanged();
			}
		};
		
	    Model.checkbox1ValueChanged = function(){
			$scope.clearAmountTotal = 0;
			var rows = new Array();
	    	var counter = 0;
			for(var index=0;index<Model.dataMaster1.rows.length;index++){
				if(Model.dataMaster1.rows[index].userdata.checked==true){
//					console.log('index-->',index);
					var row = {};
					row.id = Model.dataMaster1.rows[index].fID.value;
					row.amount = Model.dataMaster1.rows[index].orderAmountCleared.value;
					rows[counter] = row;
					$scope.clearAmountTotal = common.accAdd($scope.clearAmountTotal,Model.dataMaster1.rows[index].orderAmountCleared.value);
					counter++;
				};
			}
			return rows;
	    }

	    //表格标题中的选择框ng-model
	    $scope.checkboxValue1 = false;
	    //表格标题中的选择对话框click事件
	    Model.checkbox1Click = function () {  
			for(var index=0;index<Model.dataMaster1.rows.length;index++){
				Model.dataMaster1.rows[index].userdata.checked=$scope.checkboxValue1;
			}
			Model.checkbox1ValueChanged();
	    };    

	    
		
		//预付款单（到款）
		var options = {};
		options.container = Model;
		options.concept = 'AccountPayment';
		options.name = 'dataMaster1';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '';
		options.reader = '/invoice/accountPayment/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.defCols = {fID:{name:"fID",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},billType:{name:"billType",type:"String",label:""},billTypeName:{name:"billTypeName",type:"String",label:""},orderDate:{name:"orderDate",type:"Date",label:""},state:{name:"state",type:"String",label:""},status:{name:"status",type:"String",label:""},audit:{name:"audit",type:"String",label:""},flowType:{name:"flowType",type:"Integer",label:""},revOpay:{name:"revOpay",type:"String",label:""},direction:{name:"direction",type:"Integer",label:""},confirmDate:{name:"confirmDate",type:"Date",label:""},origin:{name:"origin",type:"String",label:""},originName:{name:"originName",type:"String",label:""},originNo:{name:"originNo",type:"String",label:""},periodStatus:{name:"periodStatus",type:"String",label:""},periodNo:{name:"periodNo",type:"String",label:""},accountPaymentNo:{name:"accountPaymentNo",type:"String",label:""},settleCode:{name:"settleCode",type:"String",label:""},settleDate:{name:"settleDate",type:"Date",label:""},dealObject:{name:"dealObject",type:"String",label:""},dealObjectLabel:{name:"dealObjectLabel",type:"String",label:""},customerID:{name:"customerID",type:"String",label:""},customerNo:{name:"customerNo",type:"String",label:""},shortName:{name:"shortName",type:"String",label:""},customerName:{name:"customerName",type:"String",label:""},accountBank:{name:"accountBank",type:"String",label:""},accountNo:{name:"accountNo",type:"String",label:""},captionID:{name:"captionID",type:"String",label:""},captionNo:{name:"captionNo",type:"String",label:""},captionName:{name:"captionName",type:"String",label:""},captionFullName:{name:"captionFullName",type:"String",label:""},isMember:{name:"isMember",type:"Integer",label:""},memberNo:{name:"memberNo",type:"String",label:""},isHaveCoupon:{name:"isHaveCoupon",type:"Integer",label:""},orderAmount:{name:"orderAmount",type:"Decimal",label:""},discountCash:{name:"discountCash",type:"Decimal",label:""},orderAmountTotal:{name:"orderAmountTotal",type:"Decimal",label:""},orderAmountCleared:{name:"orderAmountCleared",type:"Decimal",label:""},pendingAmount:{name:"pendingAmount",type:"Decimal",label:""},accountOrderAmount:{name:"accountOrderAmount",type:"Decimal",label:""},accountAmountTotal:{name:"accountAmountTotal",type:"Decimal",label:""},accountPending:{name:"accountPending",type:"Decimal",label:""},remark:{name:"remark",type:"String",label:""},cashSettle:{name:"cashSettle",type:"Integer",label:""},settleCategory:{name:"settleCategory",type:"String",label:""},settleCategoryLabel:{name:"settleCategoryLabel",type:"String",label:""},capitalID:{name:"capitalID",type:"String",label:""},capitalNo:{name:"capitalNo",type:"String",label:""},capitalName:{name:"capitalName",type:"String",label:""},capitalFullName:{name:"capitalFullName",type:"String",label:""},orderNo:{name:"orderNo",type:"String",label:""},projectNo:{name:"projectNo",type:"String",label:""},serialNumber:{name:"serialNumber",type:"String",label:""},resultStatus:{name:"resultStatus",type:"String",label:""},handling:{name:"handling",type:"String",label:""},handlingLabel:{name:"handlingLabel",type:"String",label:""},maxDetailNo:{name:"maxDetailNo",type:"Integer",label:""},employeeNo:{name:"employeeNo",type:"String",label:""},employeeName:{name:"employeeName",type:"String",label:""},comments:{name:"comments",type:"String",label:""},cUsercode:{name:"cUsercode",type:"String",label:""},cUsername:{name:"cUsername",type:"String",label:""},cDatetime:{name:"cDatetime",type:"Datetime",label:""},tUsercode:{name:"tUsercode",type:"String",label:""},tUsername:{name:"tUsername",type:"String",label:""},tDatetime:{name:"tDatetime",type:"Datetime",label:""},eUsercode:{name:"eUsercode",type:"String",label:""},eUsername:{name:"eUsername",type:"String",label:""},eDatetime:{name:"eDatetime",type:"Datetime",label:""},userId:{name:"userId",type:"String",label:""},enterpriseNo:{name:"enterpriseNo",type:"String",label:""},enterpriseName:{name:"enterpriseName",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = true;
		options.limit = -1;
		options.orderBy = "cDatetime DESC";
		
		options.onAfterRefresh = Model.dataMaster1AfterRefresh;
		options.onValueChanged = Model.dataMaster1ValueChanged;

		Model.dataMaster1 = new BizData(options);
		Model.dataMaster1.deregRows = $scope.$watch('Model.dataMaster1.rows',Model.dataMaster1.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值

		//////////////////////////////////////////////
	    
	    //--------------------------------------------------------------------------------------------
	    
	    
		
		Model.dataMaster2AfterRefresh = function(event) {
			var dataMaster2 = event.source;
			for(var i=0;i<dataMaster2.getCount();i++){
				var rowID = dataMaster2.getID(i);
				var pendingAmount = dataMaster2.getValue('pendingAmount',rowID);
				dataMaster2.setValue('orderAmountCleared',pendingAmount,rowID);
				dataMaster2.setValue('discountCash',0,rowID);
			}
		};

		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		
		Model.dataMaster2ValueChanged = function(event) {
//			console.log(event);
			if(event.column=='orderAmountCleared'){
				var dataMaster2 = event.source;
				var rowID = dataMaster2.getID(event.rowIndex);
				var orderAmountCleared = dataMaster2.getValue('orderAmountCleared',rowID);
				var pendingAmount = dataMaster2.getValue('pendingAmount',rowID);
				if(orderAmountCleared>pendingAmount){
					dataMaster2.setValue('orderAmountCleared',pendingAmount,rowID);
				}
				Model.checkbox2ValueChanged();
			}
			if(event.column=='discountCash'){
				var dataMaster2 = event.source;
				var rowID = dataMaster2.getID(event.rowIndex);
				var discountCash = dataMaster2.getValue('discountCash',rowID);
				var orderAmountCleared = dataMaster2.getValue('orderAmountCleared',rowID);//核销金额
				if(discountCash>orderAmountCleared){
					dataMaster2.setValue('discountCash',orderAmountCleared,rowID);
				}
				Model.checkbox2ValueChanged();
			}
		};
		
	    Model.checkbox2ValueChanged = function(){
			$scope.orderAmountCleared = 0;
			$scope.discountCash = 0;
			var rows = new Array();
	    	var counter = 0;
			for(var index=0;index<Model.dataMaster2.rows.length;index++){
				if(Model.dataMaster2.rows[index].userdata.checked==true){
//					console.log('index-->',index);
					var row = {};
					row.id = Model.dataMaster2.rows[index].fID.value;
					row.amount = Model.dataMaster2.rows[index].orderAmountCleared.value;
					row.discountCash = Model.dataMaster2.rows[index].discountCash.value;
					rows[counter] = row;
					$scope.orderAmountCleared = common.accAdd($scope.orderAmountCleared,row.amount);
					$scope.discountCash = common.accAdd($scope.discountCash,row.discountCash);
					counter++;
				};
			}
			return rows;
	    }

	    //表格标题中的选择框ng-model
	    $scope.checkboxValue2 = false;
	    //表格标题中的选择对话框click事件
	    Model.checkbox2Click = function () {  
			for(var index=0;index<Model.dataMaster2.rows.length;index++){
				Model.dataMaster2.rows[index].userdata.checked=$scope.checkboxValue2;
			}
			Model.checkbox2ValueChanged();
	    };    

		var options = {};
		options.container = Model;
		options.concept = 'AccountInvoice';
		options.name = 'dataMaster2';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.reader = '/invoice/accountInvoice/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.defCols = {fID:{name:"fID",type:"String",label:""},orderType:{name:"orderType",type:"String",label:""},orderTypeLabel:{name:"orderTypeLabel",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},billType:{name:"billType",type:"String",label:""},billTypeName:{name:"billTypeName",type:"String",label:""},orderDate:{name:"orderDate",type:"Date",label:""},state:{name:"state",type:"String",label:""},status:{name:"status",type:"String",label:""},audit:{name:"audit",type:"String",label:""},flowType:{name:"flowType",type:"Integer",label:""},revOpay:{name:"revOpay",type:"String",label:""},direction:{name:"direction",type:"Integer",label:""},confirmDate:{name:"confirmDate",type:"Date",label:""},origin:{name:"origin",type:"String",label:""},originName:{name:"originName",type:"String",label:""},originNo:{name:"originNo",type:"String",label:""},periodStatus:{name:"periodStatus",type:"String",label:""},periodNo:{name:"periodNo",type:"String",label:""},accountInvoiceNo:{name:"accountInvoiceNo",type:"String",label:""},invoiceCategory:{name:"invoiceCategory",type:"String",label:""},invoiceCategoryLabel:{name:"invoiceCategoryLabel",type:"String",label:""},invoiceCode:{name:"invoiceCode",type:"String",label:""},invoiceDate:{name:"invoiceDate",type:"Date",label:""},dealObject:{name:"dealObject",type:"String",label:""},dealObjectLabel:{name:"dealObjectLabel",type:"String",label:""},customerID:{name:"customerID",type:"String",label:""},customerNo:{name:"customerNo",type:"String",label:""},shortName:{name:"shortName",type:"String",label:""},customerName:{name:"customerName",type:"String",label:""},taxType:{name:"taxType",type:"Integer",label:""},taxTypeLabel:{name:"taxTypeLabel",type:"String",label:""},taxRate:{name:"taxRate",type:"Decimal",label:""},fPhone:{name:"fPhone",type:"String",label:""},taxNo:{name:"taxNo",type:"String",label:""},accountBank:{name:"accountBank",type:"String",label:""},accountNo:{name:"accountNo",type:"String",label:""},captionID:{name:"captionID",type:"String",label:""},captionNo:{name:"captionNo",type:"String",label:""},captionName:{name:"captionName",type:"String",label:""},captionFullName:{name:"captionFullName",type:"String",label:""},itemAmount:{name:"itemAmount",type:"Decimal",label:""},orderAmount:{name:"orderAmount",type:"Decimal",label:""},orderAmountOld:{name:"orderAmountOld",type:"Decimal",label:""},orderAmountTax:{name:"orderAmountTax",type:"Decimal",label:""},orderAmountTotal:{name:"orderAmountTotal",type:"Decimal",label:""},orderAmountCleared:{name:"orderAmountCleared",type:"Decimal",label:""},pendingAmount:{name:"pendingAmount",type:"Decimal",label:""},isMember:{name:"isMember",type:"Integer",label:""},memberNo:{name:"memberNo",type:"String",label:""},isHaveCoupon:{name:"isHaveCoupon",type:"Integer",label:""},discountCash:{name:"discountCash",type:"Decimal",label:""},depositRate:{name:"depositRate",type:"Decimal",label:""},accountAmountOld:{name:"accountAmountOld",type:"Decimal",label:""},accountAmountTax:{name:"accountAmountTax",type:"Decimal",label:""},accountAmountTotal:{name:"accountAmountTotal",type:"Decimal",label:""},discountRate:{name:"discountRate",type:"Decimal",label:""},fMasterID:{name:"fMasterID",type:"String",label:""},paymentTerm:{name:"paymentTerm",type:"String",label:""},paymentTermLabel:{name:"paymentTermLabel",type:"String",label:""},predictStart:{name:"predictStart",type:"Date",label:""},predictEnd:{name:"predictEnd",type:"Date",label:""},cashSettle:{name:"cashSettle",type:"Integer",label:""},next:{name:"next",type:"String",label:""},nextName:{name:"nextName",type:"String",label:""},nextNo:{name:"nextNo",type:"String",label:""},settleCategory:{name:"settleCategory",type:"String",label:""},settleCategoryLabel:{name:"settleCategoryLabel",type:"String",label:""},orderNo:{name:"orderNo",type:"String",label:""},projectNo:{name:"projectNo",type:"String",label:""},serialNumber:{name:"serialNumber",type:"String",label:""},resultStatus:{name:"resultStatus",type:"String",label:""},handling:{name:"handling",type:"String",label:""},handlingLabel:{name:"handlingLabel",type:"String",label:""},clearDatetime:{name:"clearDatetime",type:"Datetime",label:""},detailsCount:{name:"detailsCount",type:"Integer",label:""},detailsCountState:{name:"detailsCountState",type:"Integer",label:""},maxDetailNo:{name:"maxDetailNo",type:"Integer",label:""},employeeNo:{name:"employeeNo",type:"String",label:""},employeeName:{name:"employeeName",type:"String",label:""},comments:{name:"comments",type:"String",label:""},cUsercode:{name:"cUsercode",type:"String",label:""},cUsername:{name:"cUsername",type:"String",label:""},cDatetime:{name:"cDatetime",type:"Datetime",label:""},tUsercode:{name:"tUsercode",type:"String",label:""},tUsername:{name:"tUsername",type:"String",label:""},tDatetime:{name:"tDatetime",type:"Datetime",label:""},eUsercode:{name:"eUsercode",type:"String",label:""},eUsername:{name:"eUsername",type:"String",label:""},eDatetime:{name:"eDatetime",type:"Datetime",label:""},userId:{name:"userId",type:"String",label:""},enterpriseNo:{name:"enterpriseNo",type:"String",label:""},enterpriseName:{name:"enterpriseName",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
//		options.master = {"data":"$scope.dataMaster","relation":"fMasterID"};
//		options.masterData = $scope.dataMaster;
//		options.slaveDatas = null;
		options.directDeleteMode = false;
		options.limit = -1;
		options.orderBy = 'cDatetime ASC';
		
		options.onAfterRefresh = Model.dataMaster2AfterRefresh;
		options.onValueChanged = Model.dataMaster2ValueChanged;

		Model.dataMaster2 = new BizData(options);
		Model.dataMaster2.deregRows = $scope.$watch('Model.dataMaster2.rows',Model.dataMaster2.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值

		//////////////////////////////////////////////
	    
		
		Model.refresh = function(){
			if($scope.customerID==''){
				alert('请选择客户！');
			}else{

				//设置过滤条件
				Model.dataMaster1.filter = "AccountPayment.audit='confirm'"	
					+ " AND AccountPayment.status='open'"
					+ " AND AccountPayment.billType='YFKD'"
					+ " AND AccountPayment.pendingAmount>0"
					+ " AND (AccountPayment.customerID='"+$scope.customerID+"')"
					;

				//刷新数据
			    Model.dataMaster1.refreshData();

				//设置过滤条件
				Model.dataMaster2.filter = "AccountInvoice.audit='confirm'"	
					+ " AND AccountInvoice.status='open'"
					+ " AND ((AccountInvoice.billType='CGFP') OR (AccountInvoice.billType='QTYF'))"
					+ " AND AccountInvoice.pendingAmount>0"
					+ " AND (AccountInvoice.customerID='"+$scope.customerID+"')"
					;

				//刷新数据
			    Model.dataMaster2.refreshData();
				
			}
		};
		

		
	    //检查核销批号是否存在
	    $scope.writeOffNo = '';
		Model.writeOffNoOnBlur = function(event) {
//			console.log('event-->',event);
			console.log('writeOffNo-->',$scope.writeOffNo);
			
			if($scope.writeOffNo==null || $scope.writeOffNo==''){
				return;
			};
			if($scope.enterpriseNo==null || $scope.enterpriseNo==''){
				alert('请先选择供应商！');
				$scope.writeOffNo = '';
				return;
			};

			var params = {
				"writeOffNo":$scope.writeOffNo,
				"enterpriseNo":$scope.enterpriseNo
			};

			$('#writeOffNo').LoadingOverlay("show");

			$.ajax({
	             type: "POST",
	             url: $rootScope.getServiceName() + "/invoice/payFKHX/checkWriteOffNo",
	             data: JSON.stringify(params),
	             dataType: "json",
			     contentType: "application/json; charset=utf-8",
	             success: function(data, textStatus){
	 				//console.log('data-->',data);
	 				$('#writeOffNo').LoadingOverlay("hide");
					if(data.count==0){
						//不存在
//						console.log(data.count);
					}else{
						$scope.writeOffNo = '';
//						alert('核销批号已经存在！');
						systemService.dialogMsg('核销批号已经存在，请重新输入！');
					}
	             },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	 $('#writeOffNo').LoadingOverlay("hide");
	            	 alert('调用失败！',textStatus);
	             }
	         });
			
		};
		
		//到款核销
		Model.hxClick = function(event) {
//			console.log('event-->',event);
			var rows1 = Model.checkbox1ValueChanged();
			if(rows1.length<=0){
				alert('请勾选到款！');
				return;
			}
			var rows2 = Model.checkbox2ValueChanged();
			if(rows2.length<=0){
				alert('请勾选应付款！');
				return;
			}
			if($scope.orderAmountCleared==common.accAdd($scope.clearAmountTotal,$scope.discountCash)){
				if($scope.orderAmountCleared==0){
					alert('核销金额不能为0！');
					return;
				}
			}else{
				alert('核销金额不等于到款金额+现金折扣金额！');
				return;
			}
			
			if($scope.writeOffNo==undefined || $scope.writeOffNo==null || $scope.writeOffNo==''){
				alert('核销批号不能为空！');
				return;
			}
			
			//调用核销函数
			var params = {
				"revOpay":"O",
				"clearType":"6",
				"clearTypeLabel":"6.付款核销",
				"billType":"5",
				"billTypeName":"5.付款核销折扣",
				
				"writeOffNo":$scope.writeOffNo,
				"discountCash": $scope.discountCash,
				"enterpriseNo":$scope.enterpriseNo,
				"customerID":$scope.customerID,
				"rows1":rows1,
				"rows2":rows2
			};

			$.ajax({
	             type: "POST",
	             url: $rootScope.getServiceName() + "/invoice/payFKHX/insert",
	             data: JSON.stringify(params),
	             dataType: "json",
			     contentType: "application/json; charset=utf-8",
	             success: function(data, textStatus){
	 				//console.log('data-->',data);
	 				if(data.code==0){
						//执行成功
//						console.log(data.count);
						systemService.dialogMsg('核销成功！');
						//关闭窗口
						Model.close();
					}else{
//						alert('核销批号已经存在！');
						systemService.dialogMsg('核销失败！'+data.status);
					}
	             },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	 alert('调用失败！',textStatus);
	             }
	         });

			
		};

		//关闭窗口
		Model.close = function(event) {
			$rootScope.removeTab($rootScope.winid);
		};
		
	    return Model;
		
	}	
	
